package com.tl.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.tl.domain.Ball;

/**
 * 网络爬虫获取Email地址
 * 
 * @author LiangGzone
 */
public class EmailSpider {
	public static void main(String[] args) {
//		 testTable();
		// test1();
//		testTableAll();
		 test3();
	}

	public static void testTableAll() {
		URL url = null;
		try {
			// 获取URL地址
			url = new URL("http://hao123.lecai.com/lottery/draw/list/50");
			// 连接URL地址
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			urlConnection.connect();
			// 定义输入流
			InputStream in = urlConnection.getInputStream();
			InputStreamReader rea = new InputStreamReader(in, "utf-8");
			BufferedReader bur = new BufferedReader(rea);
			String len = "";
			while ((len = bur.readLine()) != null) {
				System.out.println(len);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	


	public static void testEmail() {

		URL url = null;
		String regex = "[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+";
		String tempStr = null;
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = null;
		try {
			// 获取URL地址
			url = new URL("http://zhidao.baidu.com/question/92607106.html");
			// 连接URL地址
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			urlConnection.connect();
			// 定义输入流
			InputStream in = urlConnection.getInputStream();
			byte[] buf = new byte[4096];
			while (in.read(buf) > 0) {
				tempStr = new String(buf);
				matcher = pattern.matcher(tempStr);
				System.out.println(matcher.group());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void test3() {
		
		URL url = null;
		//<option  value="2013-01-01">2013</option>
		String regex = "<option.*[value=\"]([\\w-]*)\">(\\w*)</option>";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = null;
		try {
			url = new URL("http://hao123.lecai.com/lottery/draw/list/50");
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.connect();
			InputStream in = urlConnection.getInputStream();
			InputStreamReader rea = new InputStreamReader(in, "utf-8");
			BufferedReader bur = new BufferedReader(rea);
			String len = "";
			while ((len = bur.readLine()) != null) {
				matcher = pattern.matcher(len);
				while(matcher.find()){
					System.out.println(matcher.group(2));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读取
	 */
	public static void testTable() {
		URL url = null;
		String regex = "<td class=\"td1\">([\\w-]*)</td>|<span class=\"ball_1\">([\\w-]*)</span>|<span class=\"ball_2\">([\\w-]*)</span>"; 
																																			
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = null;
		List<Ball> balls = new ArrayList<Ball>();
		try {
			// 获取URL地址
			url = new URL("http://hao123.lecai.com/lottery/draw/list/50");
			// 连接URL地址
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			urlConnection.connect();
			// 定义输入流
			InputStream in = urlConnection.getInputStream();
			InputStreamReader rea = new InputStreamReader(in, "utf-8");
			BufferedReader bur = new BufferedReader(rea);
			String len = "";

			Ball b = new Ball();
			int count = 0;

			while ((len = bur.readLine()) != null) {
				matcher = pattern.matcher(len);
				while (matcher.find()) {
					String time = matcher.group(1);
					String hq = matcher.group(2);
					String lq = matcher.group(3);
					if (StringUtils.isNotBlank(time)) {
						b = new Ball();
						b.setTime(time);
						count = 0;
					}
					if (StringUtils.isNotBlank(hq)) {
						b.hq[count] = hq;
						count++;
					}
					if (StringUtils.isNotBlank(lq)) {
						b.setHq(b.hq);
						b.setLq(lq);
						balls.add(b);
						count = 0;
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		if (balls.size() > 0) {
			for (Ball ball : balls) {
				System.out.println(ball.toString());
			}
		}

	}

	public static void test1() {

		Pattern p = Pattern.compile("([^_]+)_(\\d+)_([^_]+)");
		String src1 = "孙燕姿_20091103_遇见.mp3";
		Matcher m = p.matcher(src1);
		System.out.println(m.replaceAll("$1_$3"));
	}
}
